package com.zbkj.crmeb.store.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.common.CommonPage;
import com.common.PageParamRequest;
import com.crmeb.core.common.CommonResult;
import com.github.pagehelper.PageInfo;
import com.utils.PriceUtil;
import com.utils.StoreProductMerUtil;
import com.zbkj.crmeb.chant.model.ChannelMerchant;
import com.zbkj.crmeb.chant.service.IChannelMerchantService;
import com.zbkj.crmeb.chant.service.IChannelProductService;
import com.zbkj.crmeb.enums.ProductTypeEnum;
import com.zbkj.crmeb.store.model.CustomsRecord;
import com.zbkj.crmeb.store.model.StoreProduct;
import com.zbkj.crmeb.store.model.StoreProductNorm;
import com.zbkj.crmeb.store.request.*;
import com.zbkj.crmeb.store.response.*;
import com.zbkj.crmeb.store.service.CustomsRecordService;
import com.zbkj.crmeb.store.service.StoreBrandService;
import com.zbkj.crmeb.store.service.StoreCartService;
import com.zbkj.crmeb.store.service.StoreProductService;
import com.zbkj.crmeb.system.service.SystemConfigService;

/**
 * 商品备案表 前端控制器
 *
 */
@RestController
@RequestMapping("api/admin/store/product/record")
@Api(tags = "商品备案") //配合swagger使用
public class CustomsRecordController {
    @Autowired
    private CustomsRecordService customsRecordService;

    /**
     * 分页显示商品备案表
     *
     * @param request          搜索条件
     * @param pageParamRequest 分页参数
     */
    @ApiOperation(value = "商品备案分页列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<CustomsRecordResponse>> getRecordList(@Validated CustomsRecordSearchRequest request, @Validated PageParamRequest pageParamRequest) {

        PageInfo<CustomsRecordResponse> pageInfo = customsRecordService.getList(request, pageParamRequest);
        return CommonResult.success(CommonPage.restPage(pageInfo));
    }

    /**
     * 查询商品表信息
     * @param id Integer
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "详情")
    @GetMapping("/info/{id}")
    public CommonResult<CustomsRecord> info(@PathVariable Integer id) {
        CustomsRecord customsRecord = customsRecordService.getById(id);
        return CommonResult.success(customsRecord);
    }

    /**
     * 新增商品表
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "新增")
    @PostMapping("/save")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<String> save(@RequestBody @Validated CustomsRecord customsRecord) {
        int isHas = customsRecordService.count(Wrappers.<CustomsRecord>lambdaQuery()
                .eq(CustomsRecord :: getUniqNo, customsRecord.getUniqNo())
        );
        if(isHas > 0) {
            return CommonResult.failed("该唯一码已存在");
        }
        boolean b = customsRecordService.save(customsRecord);

        // 处理富文本
        return b ? CommonResult.success() : CommonResult.failed();
    }

    /**
     * 修改商品表
     * @param customsRecord 修改参数
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "修改")
    @PutMapping("/update")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<String> update(@RequestBody @Validated CustomsRecord customsRecord) {
        int count = customsRecordService.count(Wrappers.<CustomsRecord>lambdaQuery()
                .eq(CustomsRecord::getUniqNo, customsRecord.getUniqNo()).ne(CustomsRecord::getId, customsRecord.getId()));
        if(count > 0){
            return CommonResult.failed("该唯一码已存在");
        }

        boolean update = customsRecordService.updateById(customsRecord);

        return update ? CommonResult.success() : CommonResult.failed();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/delete/{id}")
    public CommonResult<String> delete(@PathVariable Integer id) {
        return customsRecordService.removeById(id) ? CommonResult.success() : CommonResult.failed();
    }

}
